#include <iostream>
 

 
#include <dir.h>
 
#include <cv.h>
 
#include <highgui.h>
 
#include <cvaux.h>
 

 
using namespace std;
 

 
#define WIDTH 100
 
#define HEIGHT 100
 
//cmrDetect inputClassifier    dataFolder  personID    imgNum
 
int main(int argc, char **argv)
 
{
 
    if(argc<5)
 
    {
 
        //printf("Please input like this:\ndetect flag inputImage output\n");
 
        cout<<"1";
 
        return 1;//表示参数不够，使用方法不正确
 
    }
 
    CvCapture *cmr = cvCreateCameraCapture(-1);
 
    if(cmr == NULL)
 
    {
 
        cout<<"2";
 
        return 2;
 
    }
 

 
    CvHaarClassifierCascade* cascade = (CvHaarClassifierCascade*)
 
                                       cvLoad(argv[1], 0, 0, 0 );//Load XML file
 
    if(cascade == NULL)
 
    {
 
        cout<<"3";
 
        return 3;
 
    }
 
    int ID = atoi(argv[3]);
 
    int imgNum = atoi(argv[4]);
 

 
    CvMemStorage* storage = cvCreateMemStorage(0);
 
    CvSeq *faceSeq = NULL;
 
    IplImage *img=NULL;
 
    IplImage *gray=NULL;
 
    IplImage *face = cvCreateImage(cvSize(HEIGHT,WIDTH),8,1);
 

 
    cvNamedWindow("Camera");
 

 
    CvScalar color = {0,0,255}; //blue
 

 
    if(access(argv[2],0) != 0)//检查是否存在data文件夹,返回0表示存在，-1表示不存在
 
        if( mkdir(argv[2]) != 0)//创建文件夹失败返回-1
 
        {
 
            cout<<"4";
 
            return 4;
 
        }
 
    char *IDFolder = (char *)malloc(strlen(argv[2])+10);
 
    sprintf(IDFolder,"%s\\%04d",argv[2],ID);
 
    if(access(IDFolder,0) != 0)//检查是否存在data文件夹,返回0表示存在，-1表示不存在
 
        if( mkdir(IDFolder) != 0)//创建文件夹失败返回-1
 
        {
 
            cout<<"5";
 
            return 5;
 
        }
 
    int faceNum = 0;
 
    while(1)
 
    {
 
        img = cvQueryFrame(cmr);
 
        if(img == NULL)
 
            break;
 
        gray = cvCreateImage(cvGetSize(img),8,1);
 
        cvCvtColor(img,gray,CV_RGB2GRAY);
 
        cvClearMemStorage( storage );
 
        faceSeq=(CvSeq*)cvHaarDetectObjects( gray, cascade, storage,1.2, 2, 1,cvSize(50, 50) );
 

 
        if(faceSeq == NULL || faceSeq->total <1)
 
        {
 
            continue;
 
        }
 

 
        CvRect * r = (CvRect*)cvGetSeqElem( faceSeq, faceSeq->total-1 );
 
        CvPoint start, end;
 
        start.x = r->x ;
 
        start.y = r->y ;
 
        end.x = r->x+r->width;
 
        end.y = r->y+r->height;
 
        //cout<<"Face Size: Height: "<<r->height<<"  Width: "<<r->width<<endl;
 
        cvRectangle( img, start, end, color, 2 ,8,0);
 
        cvShowImage("Camera",img);
 

 
        int keyValue = cvWaitKey(3);
 
        if(keyValue == 27)
 
            break;
 
        else if(keyValue == 32)
 
        {
 
            faceNum += 1;
 
            cvSetImageROI(gray,*r);
 
            cvResize(gray,face,CV_INTER_LINEAR);
 
            cvResetImageROI(gray);
 
            cvShowImage("face",face);
 

 
            char *imgPath = (char *)malloc(10+strlen(IDFolder));
 
            sprintf(imgPath,"%s\\%02d.bmp",IDFolder,faceNum);
 
            cvSaveImage(imgPath,face);
 
            free(imgPath);
 
            if(faceNum >= imgNum)
 
            {
 
                cvWaitKey(30);
 
                break;
 
            }
 
        }
 

 
    }
 
    //cvReleaseImage(&img);
 
    cvReleaseCapture(&cmr);
 
    cvReleaseImage(&gray);
 
    cvReleaseImage(&face);
 

 
    cout<<"0"<<endl;
 
    return 0;
 
} 
